package com.andy.base.servlet;

import com.andy.base.mapper.MappingData;

import java.io.InputStream;

public class Request {

    private String method; //GET,POST
    private String url;
    private InputStream inputStream; //socket接到的输入流
    private MappingData mappingData = new MappingData();//封装了host,context,wrapper的对象

    public Request() {
    }

    public MappingData getMappingData() {
        return mappingData;
    }

    /**
     * 解析输入流获取方法和url
     * @param inputStream
     */
    public Request(InputStream inputStream) throws Exception {
        this.inputStream = inputStream;

        // 从输入流中获取请求信息
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }

        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String inputStr = new String(bytes);
        // 获取第一行请求头信息
        String firstLineStr = inputStr.split("\\n")[0];  // GET / HTTP/1.1

        String[] strings = firstLineStr.split(" ");

        this.method = strings[0];
        this.url = strings[1];

        System.out.println("=====>>method:" + method);
        System.out.println("=====>>url:" + url);
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}
